using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Utility.System;

namespace System.Utility.Helper
{
	public class Cryptography
	{
		public const string DESKEY = "#s^XLY_DESKEY_1986,11+15";

		private const string AESSALT = "gsf4jvkyhye5/d7k8OrLgM==";

		private const string AESRGBIV = "Rkb4jvUy/ye7Cd7k89QQgQ==";

		private const int ENCRYPTSIZE = 802400;

		private const int DECRYPTSIZE = 802416;

		public Cryptography()
		{
		}

		public static string AESDecrypt(string encryptString, string key)
		{
			byte[] numArray = AESDecrypt(Convert.FromBase64String(encryptString), key);
			return Encoding.Default.GetString(numArray);
		}

		public static byte[] AESDecrypt(byte[] encryptByte, string key)
		{
			return CreateICrypto(encryptByte, key, CryptoType.Decrypt);
		}

		public static string AESDecryptFile(string decryptpath, string key)
		{
			string str = TypeExtension.TrimEnd(decryptpath, ".xly", StringComparison.Ordinal);
			try
			{
				FileStream fileStream = new FileStream(decryptpath, FileMode.Open, FileAccess.Read);
				try
				{
					if (fileStream.Length > (long)0)
					{
						FileStream fileStream1 = new FileStream(str, FileMode.OpenOrCreate, FileAccess.Write);
						try
						{
							int length = ((int)fileStream.Length - 1) / 802416 + 1;
							for (int i = 0; i < length; i++)
							{
								int num = 802416;
								if (i == length - 1)
								{
									num = (int)(fileStream.Length - (long)(i * 802416));
								}
								byte[] numArray = new byte[num];
								fileStream.Read(numArray, 0, num);
								byte[] numArray1 = AESDecrypt(numArray, key);
								fileStream1.Write(numArray1, 0, (int)numArray1.Length);
								fileStream1.Flush();
							}
							fileStream1.Close();
							fileStream1.Dispose();
						}
						finally
						{
							if (fileStream1 != null)
							{
								((IDisposable)fileStream1).Dispose();
							}
						}
						fileStream.Close();
						fileStream.Dispose();
					}
				}
				finally
				{
					if (fileStream != null)
					{
						((IDisposable)fileStream).Dispose();
					}
				}
			}
			catch (Exception exception)
			{
				throw exception;
			}
			return "";
		}

		public static string AESEncrypt(string encryptString, string key)
		{
			byte[] bytes = Encoding.Default.GetBytes(encryptString);
			return Convert.ToBase64String(AESEncrypt(bytes, key));
		}

		public static byte[] AESEncrypt(byte[] encryptByte, string key)
		{
			return CreateICrypto(encryptByte, key, CryptoType.Encrypt);
		}

		public static string AESEncryptFile(string encryptpath, string key, bool isHiddenTempFile = false)
		{
			string str = string.Concat(encryptpath, ".xly");
			if (IO.File.Exists(str))
			{
				IO.File.Delete(str);
			}
			try
			{
				FileStream fileStream = new FileStream(encryptpath, FileMode.Open, FileAccess.Read);
				try
				{
					if (fileStream.Length == (long)0)
					{
						throw new Exception("不允许对无内容文件加密!");
					}
					FileStream fileStream1 = new FileStream(str, FileMode.OpenOrCreate, FileAccess.Write);
					try
					{
						if (isHiddenTempFile)
						{
							if (File.IsValid(str))
							{
								IO.File.SetAttributes(str, FileAttributes.Hidden);
							}
						}
						int length = ((int)fileStream.Length - 1) / 802400 + 1;
						for (int i = 0; i < length; i++)
						{
							int num = 802400;
							if (i == length - 1)
							{
								num = (int)(fileStream.Length - (long)(i * 802400));
							}
							byte[] numArray = new byte[num];
							fileStream.Read(numArray, 0, num);
							byte[] numArray1 = AESEncrypt(numArray, key);
							fileStream1.Write(numArray1, 0, (int)numArray1.Length);
							fileStream1.Flush();
						}
						fileStream1.Close();
						fileStream1.Dispose();
					}
					finally
					{
						if (fileStream1 != null)
						{
							((IDisposable)fileStream1).Dispose();
						}
					}
					fileStream.Close();
					fileStream.Dispose();
				}
				finally
				{
					if (fileStream != null)
					{
						((IDisposable)fileStream).Dispose();
					}
				}
			}
			catch (Exception exception)
			{
				throw exception;
			}
			return str;
		}

		private static byte[] CreateICrypto(byte[] source, string key, CryptoType iCrypto)
		{
			byte[] array;
			if (TypeExtension.IsInvalid(source))
			{
				throw new Exception("加解密数据源不可以为空!");
			}
			if (string.IsNullOrEmpty(key))
			{
				throw new Exception("密钥不可以为空");
			}
			Rijndael rijndael = Rijndael.Create();
			try
			{
				try
				{
					MemoryStream memoryStream = new MemoryStream();
					PasswordDeriveBytes passwordDeriveByte = new PasswordDeriveBytes(key, Convert.FromBase64String("gsf4jvkyhye5/d7k8OrLgM=="));
					ICryptoTransform cryptoTransform = null;
					if (iCrypto == CryptoType.Encrypt)
					{
						cryptoTransform = rijndael.CreateEncryptor(passwordDeriveByte.GetBytes(32), Convert.FromBase64String("Rkb4jvUy/ye7Cd7k89QQgQ=="));
					}
					if (iCrypto == CryptoType.Decrypt)
					{
						cryptoTransform = rijndael.CreateDecryptor(passwordDeriveByte.GetBytes(32), Convert.FromBase64String("Rkb4jvUy/ye7Cd7k89QQgQ=="));
					}
					CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoTransform, CryptoStreamMode.Write);
					cryptoStream.Write(source, 0, (int)source.Length);
					cryptoStream.FlushFinalBlock();
					array = memoryStream.ToArray();
					memoryStream.Close();
					memoryStream.Dispose();
					cryptoStream.Close();
					cryptoStream.Dispose();
				}
				catch (IOException oException)
				{
					throw oException;
				}
				catch (CryptographicException cryptographicException)
				{
					throw cryptographicException;
				}
				catch (ArgumentException argumentException)
				{
					throw argumentException;
				}
				catch (Exception exception)
				{
					throw exception;
				}
			}
			finally
			{
				rijndael.Clear();
			}
			return array;
		}

		public static string DecodeDES(string text, string key = "#s^XLY_DESKEY_1986,11+15")
		{
			string empty;
			if (!TypeExtension.IsValid(text))
			{
				empty = string.Empty;
			}
			else if (key.Length == 24)
			{
				TripleDESCryptoServiceProvider tripleDESCryptoServiceProvider = new TripleDESCryptoServiceProvider()
				{
					Key = Encoding.ASCII.GetBytes(key),
					Mode = CipherMode.ECB,
					Padding = PaddingMode.PKCS7
				};
				ICryptoTransform cryptoTransform = tripleDESCryptoServiceProvider.CreateDecryptor();
				try
				{
					try
					{
						byte[] numArray = Convert.FromBase64String(text);
						byte[] numArray1 = cryptoTransform.TransformFinalBlock(numArray, 0, (int)numArray.Length);
						empty = Encoding.Default.GetString(numArray1);
					}
					catch
					{
						empty = string.Empty;
					}
				}
				finally
				{
					cryptoTransform.Dispose();
				}
			}
			else
			{
				empty = string.Empty;
			}
			return empty;
		}

		public static string EncodeDES(string text, string key = "#s^XLY_DESKEY_1986,11+15")
		{
			string empty;
			if (!TypeExtension.IsValid(text))
			{
				empty = string.Empty;
			}
			else if (key.Length == 24)
			{
				try
				{
					TripleDESCryptoServiceProvider tripleDESCryptoServiceProvider = new TripleDESCryptoServiceProvider()
					{
						Key = Encoding.ASCII.GetBytes(key),
						Mode = CipherMode.ECB
					};
					ICryptoTransform cryptoTransform = tripleDESCryptoServiceProvider.CreateEncryptor();
					byte[] bytes = Encoding.Default.GetBytes(text);
					byte[] numArray = cryptoTransform.TransformFinalBlock(bytes, 0, (int)bytes.Length);
					cryptoTransform.Dispose();
					empty = Convert.ToBase64String(numArray);
				}
				catch (Exception exception)
				{
					throw exception;
				}
			}
			else
			{
				empty = string.Empty;
			}
			return empty;
		}

		public static string MD5Encrypt(string value)
		{
			string str;
			if (!string.IsNullOrEmpty(value))
			{
				MD5 mD5CryptoServiceProvider = new MD5CryptoServiceProvider();
				byte[] bytes = TypeExtension.ToBytes(value, Encoding.Unicode);
				byte[] numArray = mD5CryptoServiceProvider.ComputeHash(bytes);
				StringBuilder stringBuilder = new StringBuilder();
				int length = (int)numArray.Length;
				for (int i = 0; i < length; i++)
				{
					stringBuilder.Append(numArray[i].ToString("x").PadLeft(2, '0'));
				}
				mD5CryptoServiceProvider.Clear();
				str = stringBuilder.ToString();
			}
			else
			{
				str = string.Empty;
			}
			return str;
		}

		public enum CryptoType
		{
			Encrypt,
			Decrypt
		}
	}
}